package rhc.spider.schedule;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import rhc.spider.crawler.Crawler;
import rhc.spider.request.Request;
import rhc.spider.setting.Setting;
import rhc.spider.signal.Signal;

abstract public class BasicSchedule implements Schedule {

	/**
	 * 请求容器
	 */
	protected List<Request> requestList=Collections.synchronizedList(new LinkedList<Request>());
	
	protected Crawler crawler;
	
	protected Setting setting;
	
	public BasicSchedule(Crawler crawler,Setting setting){
		this.crawler=crawler;
		this.setting=setting;
	}
	
	@Override
	public void addRequest(Request request) {
		requestList.add(request);
		crawler.getSignalManage().touchSignal(Signal.ADD_REQUEST, null);
	}
	
	@Override
	public boolean hasRequest(){
		return !requestList.isEmpty();
	}
	
	@Override
	public Iterator<Request> iterator(){
		return new RequestIterator();
	}
	
	/**获取下一个请求
	 * @return
	 */
	protected Request getNextRequest(){
		try{
			return requestList.remove(0);
		}catch(Exception e){
			return null;
		}
	}
	
	/**迭代器
	 * @author lenovo1
	 *
	 */
	private class RequestIterator implements Iterator<Request>{

		@Override
		public boolean hasNext() {
			// TODO 自动生成的方法存根
			return !requestList.isEmpty();
		}

		@Override
		public Request next() {
			return getNextRequest();
		}

		@Override
		public void remove() {
			// TODO Auto-generated method stub
			
		}
		
	}

}
